<?php

namespace App\Models;

class MaterialSnapshot extends Base
{
    protected $table = 'material_snapshots';

    const BUY_TYPE_CASH = 2;
    const BUY_TYPE_HONEY = 1;

    protected $appends = ['amount_yuan'];

    protected $fillable = [
        'user_id', 'parent_category_id', 'category_id', 'title', 'desc', 'image_file','video_file', 'url', 'buy_type', 'amount'
    ];

    protected $casts = [
        'image_file' => 'array',
        'video_file' => 'array',
    ];

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function category() {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function parent_category() {
        return $this->belongsTo(Category::class, 'parent_category_id');
    }

    public function getAmountYuanAttribute() {
        return format_yuan($this->amount);
    }


    public function setImageFileAttribute($value) {
        return $this->attributes['image_file'] = json_encode_cn($value);
    }

    public function setVideoFileAttribute($value) {
        return $this->attributes['video_file'] = json_encode_cn($value);
    }

    public function is_star() {
        return $this->hasOne(Star::class, 'entity_id')
            ->where('type', Star::TYPE_MATERIAL);
    }

    public function is_focus() {
        return $this->hasOne(Focus::class, 'target_user_id', 'user_id');
    }


}
